﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Instrument
{
    public partial class Form_Main : Form
    {
        public Ins ins;
        public AC_Source AC;
        public DC_Source DC;
		public bool ONorOFF;
		public int nTimes;
        public int nFlag = 1;


        protected string[] ACs = new string[10];
        protected string[] DCs = new string[10];

        public Form_Main()
        {
            InitializeComponent();

            ins = new Ins();
            ONorOFF = false;
            nTimes = 0;
			ACs[0] = "GPIB0::30::INSTR";
            DCs[0] = "GPIB0::29::INSTR";
        }

        private void btn_Ins_Click(object sender, EventArgs e)
        {
            ins.ShowDialog();
            if (!ins.Source_ID.Equals(""))
            {
                if (radAC.Checked)
                {
                    if(isInArray(DCs, ins.Source_ID))
                    {
                        MessageBox.Show("AC Source ID: " + ins.Source_ID + " is error!\n请重新选择。");
                        return;
                    }
                    AC = new AC_Source(ins.Source_ID);
                }
                else
                {
                    if(isInArray(ACs, ins.Source_ID))
                    {
                        MessageBox.Show("DC Source ID: " + ins.Source_ID + " is error!\n请重新选择。");
                        return;
                    }
                    DC = new DC_Source(ins.Source_ID);
                }
                //timer1.Enabled = true;
            }
        }

        private void btn_Set_Click(object sender, EventArgs e)
        {
            float fVolt = 0, 
                fValue = 0;

            try
            {
                fVolt = float.Parse(textBox_Volt.Text);
            }
            catch(Exception)
            {
                MessageBox.Show("请检查电压输入框！","Error");
            }
            try
            {
                fValue = float.Parse(textBox_Curr.Text);
            }
            catch(Exception)
            {
                MessageBox.Show("请检查电流输入框！", "Error");
            }

            if(ins.Source_ID == null ||ins.Source_ID.Equals(""))
            {
                MessageBox.Show("请先选择Source!", "Error");
            }
            else
            {
                if (radAC.Checked)
                {
                    if (null != AC)
                    {
                        if (AC.m_mbSession != null)
                            AC.Set_VF(fVolt, fValue);
                    }
                }
                else
                {
                    if (null != DC)
                    {
                        if (DC.m_mbSession != null)
                            DC.Set_VC(fVolt, fValue);
                    }
                }
                timer2.Enabled = true;
                ONorOFF = true;
                nTimes = -1;
            }
        }

        private void btn_OFF_Click(object sender, EventArgs e)
        {
            if(ins.Source_ID == null||ins.Source_ID.Equals(""))
            {
                MessageBox.Show("请先选择Source!", "Error");
            }
            else
            {
                if (radAC.Checked)
                {
                    if(null != AC)
                    {
                        if(AC.m_mbSession != null)
                            AC.ONorOFF(false);
                    }
                }
                else
                {
                    if(null != DC)
                    {
                        if(DC.m_mbSession != null)
                            DC.ONorOFF(false);
                    }
                }
            }
            ONorOFF = false;
            nTimes = 0;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Enabled = false;
			radAC.Checked = true;
            string path = System.IO.Path.Combine(Application.StartupPath, "General.ini");
        }

        private void Form_Main_FormClosed(object sender, FormClosedEventArgs e)
        {
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Interval = Convert.ToInt32(tbxInterval.Text);
            timer2.Interval = Convert.ToInt32(tbxInterval2.Text);
            float fVolt = 0,
                fValue = 0;

            try
            {
                fVolt = float.Parse(textBox_Volt.Text);
            }
            catch (Exception)
            {
                timer1.Enabled = false;
                timer2.Enabled = false;
                MessageBox.Show("请检查电压输入框！", "Error");
            }
            try
            {
                fValue = float.Parse(textBox_Curr.Text);
            }
            catch (Exception)
            {
                timer1.Enabled = false;
                timer2.Enabled = false;
                MessageBox.Show("请检查电流输入框！", "Error");
            }

            if (nFlag > 0)
            {
                if (ins.Source_ID != null)
                {
                    if (radAC.Checked)
                    {
                        if (null != AC)
                        {
                            if (AC.m_mbSession != null)
                                AC.Set_VF(fVolt, fValue);
                        }
                    }
                    else
                    {
                        if (null != DC)
                        {
                            if (DC.m_mbSession != null)
                                DC.Set_VC(fVolt, fValue);
                        }
                    }
                }
                else
                {
                    timer1.Enabled = false;
                    timer2.Enabled = false;
                    MessageBox.Show("请先选择Source!", "Error");
                }
            }
            else
            {
                if (ins.Source_ID != null)
                {
                    if (radAC.Checked)
                    {
                        if (null != AC)
                        {
                            if (AC.m_mbSession != null)
                                AC.ONorOFF(false);
                        }
                    }
                    else
                    {
                        if (null != DC)
                        {
                            if (DC.m_mbSession != null)
                                DC.ONorOFF(false);
                        }
                    }
                }
                else
                {
                    timer1.Enabled = false;
                    timer2.Enabled = false;
                    MessageBox.Show("请先选择Source!", "Error");
                }
            }

            nFlag *= -1;
            //if (checkBoxAC.Checked)
            //{
            //    if (null != AC)
            //        if (AC.m_mbSession != null)
            //        {
            //            lbl_V.Text = AC.Meas_Volt().ToString();
            //            lbl_C.Text = AC.Meas_Curr().ToString();
            //        }
            //    return;
            //}
            //else
            //{

            //    if (null != DC)
            //    {
            //        if (DC.m_mbSession != null)
            //        {
            //            lbl_V.Text = DC.Meas_Volt().ToString();
            //            lbl_C.Text = DC.Meas_Curr().ToString();
            //        }
            //    }
            //    return;
            //}
        }

        bool isInArray(string[] Array, string element)
        {
            foreach (string str in Array)
                if (str == element)
                    return true;
            return false;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            timer1.Interval = Convert.ToInt32(tbxInterval.Text);
            timer2.Interval = Convert.ToInt32(tbxInterval2.Text);
            timer1.Enabled = true;
            timer2.Enabled = true;
        }

        private void btnEnd_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            timer2.Enabled = false;
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            if (radAC.Checked)
            {
                if (null != AC)
                    if (AC.m_mbSession != null)
                    {
                        lbl_V.Text = AC.Meas_Volt().ToString();
                        lbl_C.Text = AC.Meas_Curr().ToString();
                    }
            }
            else
            {

                if (null != DC)
                {
                    if (DC.m_mbSession != null)
                    {
                        lbl_V.Text = DC.Meas_Volt().ToString();
                        lbl_C.Text = DC.Meas_Curr().ToString();
                    }
                }
            }

            if (nTimes >= 0)
                nTimes++;
            if (nTimes > 3)
                timer2.Enabled = false;
        }

        private void radAC_CheckedChanged(object sender, EventArgs e)
        {
            label2.Text = radAC.Checked ? "频率: " : "电流: ";
            textBox_Volt.Text = radAC.Checked ? "230" : "240";
            textBox_Curr.Text = radAC.Checked ? "50" : "20";
        }
    }
}
